﻿using System;
using System.IO;
using Entities;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Web;
using WebLog;

namespace Logic
{
    internal sealed class PDF
    {
        public static void Generate(Voter vc)
        {
            Document document = null;
            try
            {
                byte fontSize = 20;
                document = new Document(PageSize.LETTER);
                document.AddTitle("Comprobante Votacion");
                //TODO ugly  ....
                string path = HttpContext.Current.Server.MapPath(vc.picturePath);

                PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(path, FileMode.Create));
                writer.SetEncryption(PdfWriter.STRENGTH128BITS, null, null, PdfWriter.ALLOW_PRINTING);
                document.Open();

                Image jpeg = Image.GetInstance(HttpContext.Current.Server.MapPath("~/Images/PDFimages/logoCabezera.png"));
                jpeg.Alignment = Element.ALIGN_LEFT;
                document.Add(jpeg);

                Image waterMark = Image.GetInstance(HttpContext.Current.Server.MapPath("~/Images/PDFimages/MarcaAgua.png"));
                waterMark.SetAbsolutePosition(1, 350);
                document.Add(waterMark);

                Paragraph headParagraph = new Paragraph("Comprobante de Voto \n\n\n\n", CreateFont(FontFactory.TIMES_ROMAN, 30, Font.BOLD, BaseColor.BLACK));
                headParagraph.Alignment = Element.ALIGN_CENTER;
                document.Add(headParagraph);

                string body = StringBuilderFunctions.JoinString(vc.name, " ", vc.lastName);
                Paragraph parrafoNombre = new Paragraph("Don(ña)  ", CreateFont(FontFactory.TIMES_ROMAN, fontSize, Font.NORMAL, BaseColor.BLACK));
                parrafoNombre.Add(new Paragraph(body, CreateFont(FontFactory.TIMES_ROMAN, fontSize, Font.BOLD, BaseColor.BLACK)));
                document.Add(parrafoNombre);

                Paragraph rutParagraph = new Paragraph("Rut  ", CreateFont(FontFactory.TIMES_ROMAN, fontSize, Font.NORMAL, BaseColor.BLACK));
                rutParagraph.Add(new Paragraph(vc.rut, CreateFont(FontFactory.TIMES_ROMAN, fontSize, Font.BOLD, BaseColor.BLACK)));
                document.Add(rutParagraph);

                string message = "Su voto ha sido ingresado satisfactoriamente en nuestro sistema.\nMuchas Gracias. ";
                Paragraph centralParagraph = new Paragraph(message, CreateFont(FontFactory.TIMES_ROMAN, fontSize, Font.NORMAL, BaseColor.BLACK));
                document.Add(centralParagraph);

                string day = DateTime.Now.Day.ToString();
                string month = GetMonth(DateTime.Now.Month);
                string year = DateTime.Now.Year.ToString();

                string footer = StringBuilderFunctions.JoinString("\n\n\n\n\n\n\nSantiago (chile) ", day, " de ", month, " de ", year);
                Paragraph footerParagraph = new Paragraph(footer, CreateFont(FontFactory.TIMES_ROMAN, 12, Font.NORMAL, BaseColor.BLACK));
                footerParagraph.Alignment = Element.ALIGN_CENTER;
                document.Add(footerParagraph);

                Paragraph footerPdfIdParagraph = new Paragraph("\n\n\n\n\n\n\n" + vc.voucherVotingId, CreateFont(FontFactory.TIMES_ROMAN, 12, Font.NORMAL, BaseColor.BLACK));
                document.Add(footerPdfIdParagraph);
                document.Close();

            }
            catch (Exception e)
            {
                Logger logger = Logger.GetInstance;
                logger.WriteLog(e);
            }

            finally
            {
                document.Close();
            }
        }

        private static Font CreateFont(string type, int size, int style, BaseColor color)
        {
            Font font = new Font(FontFactory.GetFont(type, size, style, color));
            return font;
        }

        private static string GetMonth(int month)
        {
            string Month = string.Empty;
            switch (month)
            {
                case 1:
                    Month = "Enero";
                    break;
                case 2:
                    Month = "Febrero";
                    break;
                case 3:
                    Month = "Marzo";
                    break;
                case 4:
                    Month = "Abril";
                    break;
                case 5:
                    Month = "Mayo";
                    break;
                case 6:
                    Month = "Junio";
                    break;
                case 7:
                    Month = "Julio";
                    break;
                case 8:
                    Month = "Agosto";
                    break;
                case 9:
                    Month = "Septiembre";
                    break;
                case 10:
                    Month = "Octubre";
                    break;
                case 11:
                    Month = "Noviembre";
                    break;
                case 12:
                    Month = "Diciembre";
                    break;
            }

            return Month;
        }
    }
}
